package com.example.xiaosen.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.xiaosen.config.TencentCosUtil;
import com.example.xiaosen.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

/**
 * @Author: zhouxj@ferrotec.com.cn
 * @version: v1.0
 * @Date: 2025/5/14 11:56
 * @Description:
 */

@Service
public class PdfFileRepositoryImpl implements FileRepository {

    @Autowired
    private TencentCosUtil tencentCosUtil;
    @Autowired
    private IResourceService resourceService;

    @Override
    public boolean save(String chatId, Resource resource) {
        try {
            // 获取文件名
            String fileName = resource.getFilename();
            // 上传文件到腾讯云COS
            // 创建一个临时文件
            File tempFile = File.createTempFile("temp", ".pdf");
            tempFile.deleteOnExit(); // 确保程序退出时删除临时文件
            try (InputStream inputStream = resource.getInputStream();
                 FileOutputStream outputStream = new FileOutputStream(tempFile)) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
            }
            // 上传到腾讯云
            String url = tencentCosUtil.putObject(fileName, tempFile);
            // 将文件URL保存到数据库，与会话id一一映射
            com.example.xiaosen.entity.po.Resource resourceMapper = new com.example.xiaosen.entity.po.Resource();
            resourceMapper.setUrl(url);
            resourceMapper.setChatId(chatId);
            resourceMapper.setFileName(fileName);
            resourceService.save(resourceMapper);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public Resource getFile(String chatId) {
        com.example.xiaosen.entity.po.Resource resourceMapper = resourceService.getOne(new LambdaQueryWrapper<com.example.xiaosen.entity.po.Resource>()
                .eq(com.example.xiaosen.entity.po.Resource::getChatId, chatId));
        if (resourceMapper != null) {
            String urlString = resourceMapper.getUrl();
            try {
                // 创建 URL 对象
                URL url = new URL(urlString);
                // 创建 UrlResource 对象, 获取资源
                Resource resource = new UrlResource(url);
                return resource;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
